<template>
	<view class="content">
		<view class="item">
			<view style="display: flex;justify-content: center;margin-bottom: 2rem;border: none;font-size: 1.2rem;font-weight: bold;">账号登录</view>
			<view>
				<image src="/static/icon/user.png" mode=""></image>
				<input type="text" placeholder="请输入账号" v-model="form.id"/>
			</view>
			<view>
				<image src="/static/icon/pwd.png" mode=""></image>
				<input type="password" placeholder="请输入登录密码" v-model="form.password"/>
			</view>
			<view style="border: none;">
				<button type="default" style="width: 100%;background-color: #f9ae1f;color: #fff;height: 2.1rem;line-height: 2.1rem;font-weight: bold;border-radius: 15px;" @click="doLogin()">登录</button>
			</view>
			<view style="border: none;">
				没有账号,<text style="color: #3d87f2;" @click="jumpPage()">立即注册</text>
			</view>
		</view>
		
	</view>
</template>

<script>
	export default {
		data() {
			return {
				form:{
					id:null,
					password:null
				},
				userInfo:null
			};
		},
		onLoad(params) {
			this.getUserInfo()
		},
		methods:{
			getUserInfo(){
				let that = this
				if(that.userInfo != null){
					uni.showToast({
						title:'以登录,无需重复登录',
						icon:'none',
						success() {
							uni.switchTab({
								url:'/pages/index/index',
							})
						}
					})
				}
			},
			jumpPage(){
				uni.redirectTo({
					url:'/pages/register/register'
				})
			},
			doLogin(){
				let that = this
				uni.request({
					url:getApp().globalData.baseUrl + '/api/user/login',
					method:'POST',
					data: that.form,
					success(res) {
						if(res.data.code == 1){
							uni.showToast({
								title:res.data.msg,
								icon:'none',
								success(sres) {
									console.log(res.data)
									uni.setStorage({
										key:'user_token',
										data: res.data.data.userinfo.token,
										success() {
											uni.switchTab({
												url:'/pages/index/index'
											})
										}
									})
									
								}
							})
						}else{
							uni.showToast({
								icon:'none',
								title:res.data.msg
							})
						}
						
					},
					fail() {
						uni.showToast({
							title:'组件错误',
							icon:'none',
						})
					}
				})
			}
		}
	}
</script>

<style lang="scss">
page{
	background: #f4f4f4;
}
.content{
	padding: 1rem;
	margin-top: 1rem;
}
.item{
	background-color: #fff;
	padding: 1rem;
	border-radius: 15px;
	box-shadow: 0px 0px 15px rgba(5, 94, 93, 0.1);
	
}
.item>view{
	display: flex;
	align-items: center;
	padding: .5rem;
	border-bottom: 1px solid #f4f4f4;
	margin: 4px 1rem;
}
.item>view>input{
	font-size: 14px;
}

image{
	width: 1rem;
	height: 1rem;
	margin-right: 1rem;
}
</style>
